home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15362 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.1 KB  |  82 lines

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: alt.msdos.programmer,comp.lang.c,comp.lang.pascal.misc
  4. Subject: Re: typecasting preferences
  5. Date: Thu, 18 Apr 96 15:20:07 GMT
  6. Organization: none
  7. Distribution: world
  8. Message-ID: <829840807snz@genesis.demon.co.uk>
  9. References: <4l020g$i9j@srvr1.engin.umich.edu> <Pine.A32.3.91.960416145209.106109C-100000@black.weeg.uiowa.edu> <4l4ldb$nkl@srvr1.engin.umich.edu>
  10. Reply-To: fred@genesis.demon.co.uk
  11. X-NNTP-Posting-Host: genesis.demon.co.uk
  12. X-Newsreader: Demon Internet Simple News v1.27
  13. X-Mail2News-Path: genesis.demon.co.uk
  14.  
  15. In article <4l4ldb$nkl@srvr1.engin.umich.edu>
  16.            hasdi@news-server.engin.umich.edu "HASDI RODZMANN HASHIM" writes:
  17.  
  18. >The way I understand it, type-conversion is to transform the bits of one
  19. >variable so that it is representable as the type to be converted into. eg. 
  20. >(int)my_float. type-casting doesn't transform the bits at all, but force
  21. >the compiler to treat that variable as the specified type... which is
  22. >highly unportable. Why would anyone want to typecast then? Can somebody
  23. >clarify this for me? 
  24.  
  25. You've misunderstood. Casting is simply a way of forcing type conversions
  26. explicitly - changes of representation i.e. the bits patterns or even the
  27. number of bits occupied by the value are in general required. For example
  28.  
  29.     int i;
  30.     double d;
  31.  
  32.     i = 1;
  33.  
  34.     x = i;
  35.  
  36. This involves an implicit conversion and the result will be that x contains
  37. 1.0 (or something as close as possible to it).
  38.  
  39.     x = (double) i;
  40.  
  41. has exactly the same result. However the conversion to double is performed
  42. explicitly by the cast instead of implicitly by the assignment. Pointer
  43. casts can also result in a change of representation for the pointer. However
  44. there is no effect in what is being pointed to so you can end up with a
  45. pointer pointing to something that was created as a different type. E.g.
  46.  
  47.     int x = 1;
  48.     char *p = (char *)&x;
  49.  
  50. &x is a pointer to int which is cast to a pointer to char. The representation
  51. of the pointer may change if the system uses different representations
  52. fort int * and char * (some systems do). If you dereference p you will
  53. be accessing what it points to as a char although the value containted in x
  54. is represented as an int.
  55.  
  56.  
  57. >As a side note, using typedef so that you can use "myrecord" instead of
  58. >"struct myrecord_s" has been a real major bug to me.
  59.  
  60. Why? Structure tags allow you tag the fundamental structure types. typedefs
  61. are a general mechanism in C to create aliases for any type including
  62. structures.
  63.  
  64. >Unfortunately, this
  65. >is necessary to avoid shift/reduce conflicts.
  66.  
  67. You may have to define the grammar differently but I see no need for
  68. conflicts. The main issue is that in C tags have their own namespace. That
  69. is only possible if they can be distinguished from other types of
  70. identifier syntactically.
  71.  
  72. >Makes me wonder how C++ parser works.
  73.  
  74. There's nothing special here however C++ doesn't give tags a separate
  75. namespace.
  76.  
  77. -- 
  78. -----------------------------------------
  79. Lawrence Kirby | fred@genesis.demon.co.uk
  80. Wilts, England | 70734.126@compuserve.com
  81. -----------------------------------------
  82.